package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.project.domain.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.ServiceContractMapper;
import com.hngates.project.domain.ServiceContract;
import com.hngates.project.service.IServiceContractService;
import com.hngates.common.core.text.Convert;

/**
 * 劳务合同Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-24
 */
@Service
public class ServiceContractServiceImpl implements IServiceContractService {
    @Autowired
    private ServiceContractMapper serviceContractMapper;

    /**
     * 查询劳务合同
     *
     * @param id 劳务合同主键
     * @return 劳务合同
     */
    @Override
    public ServiceContract selectServiceContractById(Long id) {
        return serviceContractMapper.selectServiceContractById(id);
    }

    /**
     * 查询劳务合同列表
     *
     * @param serviceContract 劳务合同
     * @return 劳务合同
     */
    @Override
    public List<ServiceContract> selectServiceContractList(ServiceContract serviceContract) {
        return serviceContractMapper.selectServiceContractList(serviceContract);
    }

    /**
     * 新增劳务合同
     *
     * @param serviceContract 劳务合同
     * @return 结果
     */
    @Override
    public int insertServiceContract(ServiceContract serviceContract) {
        serviceContract.setCreateTime(DateUtils.getNowDate());
        return serviceContractMapper.insertServiceContract(serviceContract);
    }

    /**
     * 修改劳务合同
     *
     * @param serviceContract 劳务合同
     * @return 结果
     */
    @Override
    public int updateServiceContract(ServiceContract serviceContract) {
        serviceContract.setUpdateTime(DateUtils.getNowDate());
        return serviceContractMapper.updateServiceContract(serviceContract);
    }

    /**
     * 批量删除劳务合同
     *
     * @param ids 需要删除的劳务合同主键
     * @return 结果
     */
    @Override
    public int deleteServiceContractByIds(String ids) {
        return serviceContractMapper.deleteServiceContractByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除劳务合同信息
     *
     * @param id 劳务合同主键
     * @return 结果
     */
    @Override
    public int deleteServiceContractById(Long id) {
        return serviceContractMapper.deleteServiceContractById(id);
    }

    /**
     * @param serviceContractList 劳务合同信息
     * @param updateSupport       是否支持更新 如果已经存在 则进行更新数据
     * @param operName            操作用户
     * @return
     */
    @Override
    public String importServiceContract(List<ServiceContract> serviceContractList, Boolean updateSupport, String operName) {
        if (Objects.isNull(serviceContractList) || serviceContractList.size() == 0) {
            throw new BusinessException("导入劳务合同不能为空");
        }

        StringBuilder importMsg = new StringBuilder();

        //循环Excel中的记录
        for (ServiceContract serviceContract : serviceContractList) {
            try {
                //constructionUnit是循环中 当前的记录对象
                //根据当前的Excel中的对象去数据库中查 查询时 以Excel中名称作为参数去查 返回值是 与项目同名的记录转为数据库对象
                ServiceContract serviceContract1 = serviceContractMapper.selectServiceContractByContractMoney(serviceContract.getContractMoney());
                //判断数据库对象是否存在
                if (serviceContract1 == null) {
                    //如果不存在就创建
                    serviceContract1 = new ServiceContract();
                    //给对象的字段名赋值
                    serviceContract1.setContractMoney(serviceContract.getContractMoney());
                    serviceContract1.setPaymentMethod(serviceContract.getPaymentMethod());
                    serviceContract1.setUnpaidBalance(serviceContract.getUnpaidBalance());
                    serviceContract1.setPaymentDate(serviceContract.getPaymentDate());

                    //赋值完成后 将这个对象插入到数据库中
                    serviceContractMapper.insertServiceContract(serviceContract1);

                    importMsg.append("<br/> " + "劳务合同" + serviceContract1.getPaymentMethod() + "导入成功");

                } else if (updateSupport) {
                    serviceContract1.setUpdateBy(operName);
                    serviceContract1.setPaymentMethod(serviceContract.getPaymentMethod());
                    serviceContract1.setUnpaidBalance(serviceContract.getUnpaidBalance());
                    serviceContract1.setPaymentDate(serviceContract.getPaymentDate());
                    serviceContractMapper.updateServiceContract(serviceContract1);

                    importMsg.append("<br/> " + "劳务合同" + serviceContract1.getPaymentMethod() + "更新成功");

                } else {

                    importMsg.append("<br/> " + "劳务合同" + serviceContract1.getPaymentMethod() + "已存在");

                }

            } catch (Exception e) {
                importMsg.append("<br/> " + "劳务合同" + serviceContract.getPaymentMethod() + "导入时异常");

            }
        }
        return importMsg.toString();
    }
}
